/**
 * @name Ccay.Registry.ListPropertyValidation
 * @description UserGroup 数据字典检查器显示页面
 * @author chaowangbang
 * @since 2014-11-22
 * @param
 */
Ccay.Module.define("Ccay.Registry.listRegistryValidation",function(page, $S) {
	/**
	 * 单选弹出框页面初始化
	 */
	page.ready = function() {
		page.addStringPrototype();
		//ValidateResult列转换器,并设置各类型时显示颜色
		var rendValidateResult = function(value, rowData) {
			var res, temp = "<font color='{1}'>{0}</font>";
			res = convertI18N(value, rowData);
			if (res != null) {
				//设置各类型显示颜色
				if ('PASS' == rowData.isValidate) {
					return temp.formatText(res, 'green');
				} else if ('WARN' == rowData.isValidate) {
					return temp.formatText(res, 'orange');
				} else if ('ERROR' == rowData.isValidate) {
					return temp.formatText(res, 'red');
				} else {
					return res;
				}
			}
			return null;
		};

		//公共列值国际化转化方法
		var convertI18N = function(value, rowData) {
			var param = null;
			if (value != null) {
				try {
					param = eval(rowData.resultParam);
				} catch (e) {
					param = rowData.resultParam;
				}
				return $i18n(value).formatTextArray(param);
			} else {
				return null;
			}
		}

		var rendSuggest = function(value, rowData) {
			return convertI18N(value, rowData);
		};

		var gridOptions = {
			height : 300,
			allowFreezen : true,
			showToolBar : true,
			customSetting : true,
			toolPanel : "#ccay-grid-toolbar",
			editMode : true,
			showColumnIcon : false,
			columns : [{
						field : "name",
						width : 200,
						freezeable : true,
						header : "ccay.registry.label.name",
						editable : false,
						content : "${#Ccay.Registry.listRegistryValidation.renderNameColumn(data)}"
					},  {
						field : "value",
						width : 150,
						header : "ccay.security.label.dictionaryValue",
						editable : false,
						renderer : function(value) {
							if (value == null || value == "") {
								return $i18n("ccay.registry.validator.label.noconfig");
							} else {
								return value;
							}
						}
					}, {
						field : "referenceValue",
						width : 150,
						header : "ccay.registry.label.referenceValue"
					},{
						field : "status",
						width : 80,
						cssName: "center",
						header : "ccay.registry.validation.enable",
						editable : false,
						renderer : function(value) {
							var statusTemp = "<a class='ccay-icon {1} nocursor' title='{0}'></a>"
							if (value == "1") {
								return statusTemp.formatText($i18n("ccay.common.label.yes"), 'yes') ;
							} else {
								return statusTemp.formatText($i18n("ccay.common.label.no"), 'no') ;
							}
						}
					}, {
						field : "validateResult",
						width : 200,
						header : "ccay.registry.label.ValidateResult",
						renderer : rendValidateResult,
						editable : false
					}, {
						field : "suggest",
						width : 200,
						header : "ccay.registry.validation.suggestion",
						renderer : rendSuggest,
						editable : false
					}],
			ajax : {
				url : 'services/ccay/registry/validation/find/page/{0}/{1}'
			},
			operation : {
				search : {
					btn : '#btnSearch',
					panel : '#registerValidateForm'
				},
				save : {
					btn : '#btnSave',
					// 更新请求地址
					ajax : {
						url : 'services/ccay/registry/validation/checkRestry'
					}
				}

			}

		};
		$S("#registryValidateGrid").igrid(gridOptions);
	}

	page.clearMsg = function() {
		//清理全部的校验信息
		Ccay.doDelete('services/ccay/registry/validation/delete/all',
				{}, function() {
					$S('#registryValidateGrid').manager().reload(true,
							true);
				});
	}

	page.validateCurPage = function() {
		var p = $S('#registryValidateGrid').manager().getPageInf(); // .pageSize .curPage
		//校验当前页数据
		Ccay.doGet(
				"services/ccay/registry/validation/page/validate/{0}/{1}"
						.formatURI(p.pageSize, p.curPage),
				$S("#registerValidateForm").jsonData(), function() {
					//校验完成后刷新本页
					$S('#registryValidateGrid').manager().reload(true,
							true);
				});
	}
	
	page.addStringPrototype = function(){
		// 自定义支持传入参数为数组的格式化方法
		String.prototype.formatTextArray = function(data) {
			var args = arguments, reg, text;
			reg = /\{(\d+)\}/g;
			if (args.length == 1 && $.isArray(data)) {
				// reg = /\${([\s\S]*?)}/g;
				args = data;
			}

			return this.replace(reg, function(m, i) {
						text = args[i];
						return text == null ? '' : text;
					});
		}
	}
	
	page.renderNameColumn = function(record){
		var desc = record.description || $i18n("ccay.registry.validator.tip.description.none");
		return "<a href='#' title='{0}' style='text-decoration: none;'>{1}</a>".formatValue(desc, record.name);
	}
});
